package uy.com.tsi2G7.superbet.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import uy.com.tsi2G7.superbet.event.Event;
import uy.com.tsi2G7.superbet.event.EventMarket;
import uy.com.tsi2G7.superbet.event.EventMarketResult;
import uy.com.tsi2G7.superbet.event.EventService;
import uy.com.tsi2G7.superbet.event.EventType;
import uy.com.tsi2G7.superbet.user.User;
import uy.com.tsi2G7.superbet.user.UserService;

public class AndroidServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	@EJB
	private UserService userService;
	@EJB
	private EventService eventService;
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		HttpSession session = request.getSession(true);
		
		String operation = (String) request.getParameter("operation");
		if(operation.equals("login")){
			try {
				User user = userService.validateCredentials(request.getParameter("user"), request.getParameter("password"));
				List<EventType> eventTypes = eventService.getAllEventTypes();
				List<Event> events = eventService.getAllEvents();
				String eventTypesString = "EventTypes=";
				for (EventType eventType : eventTypes) {
					eventTypesString=eventTypesString+eventType.getId()+"--"+eventType.getName()+",,";
				}
				String eventsString = "Events=";
				for (Event event : events) {
					eventsString=eventsString+event.getEventTypeId()+"&"+event.getId()+"--"+event.getName()+",,";
				}
				//response.setContentType("text");
				response.setLocale(new Locale("ES"));
				 PrintWriter out = response.getWriter();
				 out.println("Nombre="+user.getName()+"|");
				 out.println(eventTypesString+"|");
				 out.println(eventsString+"|");
				 
			} catch (Exception e) {
				 PrintWriter out = response.getWriter();
				 out.println("Error="+e.getMessage()+"|");
			
			}
		}else if(operation.equals("getMarkets")){
			Map<EventMarket, List<EventMarketResult>> markets = eventService.getCurrentMarkets(
					Long.valueOf(request.getParameter("event")+"")
					, Long.valueOf(request.getParameter("eventType")+""));
			String marketsString = "Markets=";
			String resultsString = "Results=";
			int index=0;
			for (EventMarket market : markets.keySet()) {
				if(index++==10)
					break;
				marketsString=marketsString+market.getMarketId()+"--"+market.getName()+" "+new SimpleDateFormat("dd/MM/yy HH:mm").format(market.getStartDate())+",,";
				for (EventMarketResult result : markets.get(market)) {
					resultsString=resultsString+market.getMarketId()+"&"+result.getId()+"--"+result.getName()+",,";
				}
			}
			response.setLocale(new Locale("ES"));
			 PrintWriter out = response.getWriter();
			 out.println(marketsString+"|");
			 out.println(resultsString+"|");
			
		}
	}

}
